//导入模块
import {load,isLogin} from "/code/admin/util/LoadView.js"
load("addnews")
let photo = ""
let content = ""
//富文本编辑器
const { createEditor, createToolbar } = window.wangEditor

const editorConfig = {
    placeholder: '请输入内容',
    onChange(editor) {
        const html = editor.getHtml()
        //   console.log(html)
        // 也可以同步到 <textarea>
        content = html
        // console.log(content)
    }
}

const editor = createEditor({
    selector: '#editor-container',
    html: '<p><br></p>',
    config: editorConfig,
    mode: 'default', // or 'simple'
})

const toolbarConfig = {}
//富文本编辑器的工具栏
const toolbar = createToolbar({
    editor,
    selector: '#toolbar-container',
    config: toolbarConfig,
    mode: 'default', // or 'simple'
})
//添加封面图片时，转换成base64格式
CoverImage.onchange = function(evt){
    let reader = new FileReader()
    reader.readAsDataURL(evt.target.files[0])
    reader.onload = function(e){
        photo = e.target.result
    }
    // console.log(photo)
}
//创建新闻表单绑定提交事件
CreateNewsForm.onsubmit = async function(evt){
    evt.preventDefault()
    let user = JSON.parse(isLogin())
    await fetch("http://localhost:3000/news",{
        method:"POST",
        headers:{
            "Content-Type":"application/json"
        },
        body:JSON.stringify({
            author:user.username,
            title:Newstitle.value,
            content,
            type:category.value,
            photo,
        })
    }).then(res => res.json())
    location.href = "/code/admin/view/news-manage/NewsList/index.html"

}